package com.li.shanlin.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.li.shanlin.baseUtils.FacePlusUtils;
import com.li.shanlin.utils.BaseFileUtil;

@Controller
@RequestMapping("/boota")
public class TestController {
	@RequestMapping("/test")
	@ResponseBody
	public String liyinLong(@RequestParam("shenfen") MultipartFile shenfen, HttpServletRequest request){
		String path = request.getSession().getServletContext().getRealPath("/")+"static/upload/liyinlong.jpg";
		if(!shenfen.isEmpty()){
			try {
				shenfen.transferTo(new File(path));
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		String shenfens = null;
		try {
			shenfens = BaseFileUtil.encodeBase64File(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.getSession().setAttribute("shenfen", shenfens);
		//return "redirect:/boota/test1";
		return shenfens;
	}
	@RequestMapping("/test1")
	@ResponseBody
	public String liyinLong1(HttpServletRequest request) throws Exception{
		String shenfen = request.getParameter("shenfen");
		shenfen = shenfen.replaceAll(" ", "");
		System.out.println("shenfen:"+shenfen);
		//String shenfen = (String) request.getSession().getAttribute("shenfen");
		String targetPath = request.getSession().getServletContext().getRealPath("/")+"static/upload/liyin.jpg";
		System.out.println("targetPath:"+targetPath);
		BaseFileUtil.decoderBase64File(shenfen, targetPath);
		File ffs = new File(targetPath);
		String cardInfo = FacePlusUtils.faceOcridcard(ffs);
		return cardInfo;
	}
	@RequestMapping("/test2")
	@ResponseBody
	public String liyinLong2(@RequestParam("shenfen") MultipartFile shenfen, HttpServletRequest request){
		String path = request.getSession().getServletContext().getRealPath("/")+"static/upload/liyinlon.jpeg";
		String path1 = request.getSession().getServletContext().getRealPath("/")+"static/upload2/liyinlong1.jpeg";
		System.out.println("path:" + path);
		System.out.println("path1" + path1);
		if(!shenfen.isEmpty()){
			try {
				shenfen.transferTo(new File(path));
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
		}
		String shenfens = null;
		try {
			shenfens = BaseFileUtil.encodeBase64File(path);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("shenfens:-->"+shenfens);
		try {
			BaseFileUtil.decoderBase64File(shenfens, path1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File ffs = new File(path1);
		String cardInfo = FacePlusUtils.faceOcridcard(ffs);
		return cardInfo;
	}
}
